<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use think\Session;

class Configure extends AdminBase
{
	public $Config;

    public function _initialize() {
        parent::_initialize();

        $this->Config = app_model("admin","Config");
    }


    public function income()
    {
        //收款银行配置
        $list = $this->Config->getList("id,value,remark","income");
        $weixin = $this->Config->getList("id,value,remark","weixin");
        $alipay = $this->Config->getList("id,value,remark","alipay");
        $alipayM = $this->Config->getList("id,value,remark","alipay_m");
        $SMS = $this->Config->getList("id,value,remark","SMS");


        $this->assign("list",$list); 
        $this->assign("weixin",$weixin);
        $this->assign("alipay",$alipay);
        $this->assign("alipay_m",$alipayM);
        $this->assign("SMS",$SMS);
        return $this->fetch();
        return $this->fetch();
    }

    //网站信息
    public function web_intercalate(){

        $list = $this->Config->getList("id,value,remark","web");
        //print_r($list);
        $this->assign("list",$list);
        return $this->fetch();
    }

    //微信
    public function weixin(){
        
    }


    //修改配置
    public function save_config(){

        if( request()->isAjax() ){

            $map = input("post.");

            $data = [];
            $type = 1;
            foreach($map as $k=>$v){
                if( strpos($k,"-") !== false ){
                    //多条
                    //print_r($k);
                    $type = 2;
                    $Arr = explode("-",$k);
                    $data[$Arr[0]][$Arr[1]] = $v;
                }else{
                    //单条
                    $data[$k] = $v;
                }
            }
            if( $type == 1 ){

                $this->Config->update($data);
            }else{

                $this->Config->isUpdate()->saveAll($data);
            }

            $retArr["result"] = 0;
            $retArr["msg"] = "修改成功";
            $this->json_return($retArr);
        }

    }

    //套餐图片上传
    public function up_image(){

        $file = request()->file('file');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . '/public' . DS . 'Uploader/Logo');
            if($info){
                $retArr = [
                    "code"=>0,
                    "msg" => array("imgUrl" => '/Uploader/Logo/'.$info->getSaveName()),
                ];
            }else{
                $retArr = [
                    "code"=>1,
                    "msg" => array("error" => $file->getError()),
                ];
                // 上传失败获取错误信息
            }
            echo json_encode($retArr);
            exit();
        }
    }
}
